
#include <stdio.h>
#include <stdlib.h>
#include <libspe2.h>

#include "cmatrixprod.h"


/* Cette fonction, appelé par le main, s'éxécute sur le PPE et va lancer des 
   programmes sur les SPEs */

int cmatrixprod(int M, int N, int P,t_complex *A, 
		 t_complex *B,t_complex *C){
    int ret;

    spe_context_ptr_t spe;
    spe_program_handle_t *prog;
    unsigned int entry;
    spe_stop_info_t stop_info;

    abs_params_t abs_params __attribute__((aligned(16))); // or 16?? (orig value)

    prog = spe_image_open("cmatrixprod_spe");
    if (!prog) {
        perror("spe_image_open");
        exit(1);
    }

    spe = spe_context_create(0, NULL);
    if (!spe) {
        perror("spe_context_create");
        exit(1);
    }

    ret = spe_program_load(spe, prog);
    if (ret) {
        perror("spe_program_load");
        exit(1);
    }

    abs_params.size_M = M;
    abs_params.size_N = N;
    abs_params.size_P = P;
    abs_params.in_matrix_A = (long long)A;
    abs_params.in_matrix_B = (long long)B;
    abs_params.out_matrix_C = (long long)C;

    printf("__CMATRIXPROD - appel le spe\n");

    printf("__CMATRIXPROD - size of abs_params : %d\n",(int)sizeof(abs_params));

    entry = SPE_DEFAULT_ENTRY;
    ret = spe_context_run(spe, &entry, 0, &abs_params, NULL, &stop_info);
    if (ret < 0) {
        perror("spe_context_run");
        exit(1);
    }

    ret = spe_context_destroy(spe);
    if (ret) {
        perror("spe_context_destroy");
        exit(1);
    }

    ret = spe_image_close(prog);
    if (ret) {
        perror("spe_image_close");
        exit(1);
    }

    return 0;
}
